dojo.require("dojo.date.stamp");

var changing = false;

function editBlur(entry){
    if (changing){
	var box=dojo.byId(this.id+"_field");
	var boxContents=box.value;
	if (boxContents==""){
            boxContents="edit this entry";
	}
	var origId=this.id;
	var entryId=origId.replace(/^.*Edit(.*)$/,"$1");
	var entryType=origId.replace(/^(.*)Edit.*$/,"$1");
	dojo.xhrPost({
		url: "log_entries.pl",
		sync: true,
		content: {action: "end_modify",
			entry_id: entryId,
			entry_type: entryType,
			entry_value: boxContents},
		load: function(data,ioargs){
		    boxContents=data;
		},
		error: function(error,ioargs){
		    alert(error);
		}
	    });
	var newEntry=boxContents.replace(/\n/g,"<br>");
	this.innerHTML=newEntry;
	changing=false;
    }
}

function editBox(entry){
    if (!changing){
	var oldEntry=this.innerHTML;
	var newEntry=oldEntry.replace(/\<br\>/g,"\n");
	var origId=this.id;
	var entryId=origId.replace(/^.*Edit(.*)$/,"$1");
	changing=true;
	// mark the database entry as being edited
	dojo.xhrPost({
		url: "log_entries.pl",
	        sync: true,
	        content: {action: "start_modify",
			entry_id: entryId},
		load: function(data,ioargs){
		    if (data==1){
			// this entry is already being edited
			alert("this log entry is being edited elsewhere");
			changing=false;
		    }
		},
	        error: function(error,ioargs){
		    alert(error);
		}
	    });
	if (changing){
	    this.innerHTML="<textarea name='textarea' id='"+this.id+"_field' "+
		"class='editingbox'>"+
		newEntry+"</textarea>";
	    var myBox=dojo.byId(this.id+"_field");
	    dojo.connect(myBox,'onblur',this,editBlur);
	    myBox.focus();
	}
    }
}

function dbAddEntry() {
    dojo.xhrPost({
	    url: "log_entries.pl",
	    sync: "true",
	    form: "page_form",
	    content: {action: "new"},
	    load: addEntry,
	    error: function(error,ioargs){
		alert(error);
	    }
	});
}

function addEntry(data,ioargs){
    var container = dojo.byId("logEntries");
    var newRow = dojo.create("tr");
    
    // split the data into its components
    var entryArray=data.split("|");
    var timeArray=entryArray[2].split(" ");
    var fullTimeString=timeArray[0]+"<br />"+timeArray[1];
    var editId=entryArray[0];
    
    var newTimeEditable=dojo.create("span",{innerHTML: fullTimeString, id: "timeEdit"+editId});
    var newTime=dojo.create("th");
    newTime.appendChild(newTimeEditable);
    var newEditable=dojo.create("span",{innerHTML: entryArray[1], id: "entryEdit"+editId});
    var newEntry = dojo.create("td");
    newEntry.appendChild(newEditable);

    newRow.appendChild(newTime);
    newRow.appendChild(newEntry);
    container.appendChild(newRow);
    
    var editableSpan=dojo.byId("timeEdit"+editId);
    dojo.connect(editableSpan,'onclick',editableSpan,editBox);
    editableSpan=dojo.byId("entryEdit"+editId);
    dojo.connect(editableSpan,'onclick',editableSpan,editBox);
    
}

function updateEntries(){
    dojo.xhrPost({
	    url: "log_entries.pl",
	    sync: "true",
	    form: "page_form",
	    content: {action: "get_all"},
	    load: function(data,ioargs){
		var lineArray=data.split(/\n/);
		for (var i=0;i<lineArray.length;i++){
		    var elArray=lineArray[i].split("|");
		    // the array has the order:
		    // entry_id,time,log_entry,user_editable,entry_type
		    // check that this entry doesn't already exist on the page
		    if (elArray[3]==1){
			var checkNodeEntryName="entryEdit"+elArray[0];
			var checkNodeTimeName="timeEdit"+elArray[0];
		    }
		    var entryNode=dojo.byId(checkNodeEntryName);
		    var timeNode=dojo.byId(checkNodeTimeName);
		    if (!entryNode){
			// need to add this node
			if (elArray[3]==1){
			    var elArgs=elArray[0]+"|"+elArray[2]+"|"+
				elArray[1];
			    addEntry(elArgs,"");
			}
		    }
		}
	    },
	    error: function(error,ioargs){
		alert(error);
	    }
	});
}

function init(){
    var entryAdd=dojo.byId("AddEntry");
    dojo.connect(entryAdd,'onclick',null,dbAddEntry);

    // get the entries in the database
    updateEntries();
}

dojo.addOnLoad(init);
